﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace PPI.Data.Inspection
{
    public class DInspectionImage
    {
        public static DataSet SelectAll(string connectionKey, Int32 inspectionFk)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Inspection_Images_SelectAll", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                if(inspectionFk != Int32.MinValue)
                    da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XMissionStructure_PoleFk", ParameterDirection.Input, false, inspectionFk));
                else
                    da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XMissionStructure_PoleFk", ParameterDirection.Input, true, DBNull.Value));
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Inspection_Images_SelectAll", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static int Insert(Int32 Inspection, Int32 InspectionResult, string filename, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Inspection_Images_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Inspection
                if (Inspection == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XMissionStructure_PoleFk", ParameterDirection.Input,true, DBNull.Value));
                }else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XMissionStructure_PoleFk", ParameterDirection.Input, false, Inspection));
                }
                #endregion

                #region InspectionResult
                if (InspectionResult == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ResultFk", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ResultFk", ParameterDirection.Input, false, InspectionResult));
                }
                #endregion

                cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, false, filename));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ImageId", ParameterDirection.ReturnValue, false, 0));
                cmd.ExecuteNonQuery();
                tr.Commit();
                return (Int32)cmd.Parameters["@Vendor_InspVisit_XmissionStructures_ImageId"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void UpdateFileName(Int32 imageId,string filename, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Inspection_Images_UpdateFileName", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@imageId", ParameterDirection.Input, false, imageId));
                cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, false, filename));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Delete(Int32 imageId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Inspection_Images_Delete", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@InspectionImageId", ParameterDirection.Input, false, imageId));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                throw ex;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }

        }
    }
}
